



function addMaterialToSellOrder(){

	//得到要添加记录的表格---tbody
	var mbody = document.getElementById("mbody");
	//向表格中增加一行记录
	var tr = mbody.insertRow(mbody.rows.length) ;
	//表示行的下标
	var rowIndex = mbody.rows.length-1 ;
	
	//增加一个单元格
	
	var cell = tr.insertCell(0); 
	
	cell.innerHTML = "<td><input type='hidden' name='sellorderdetail' id='sellorderdetail"+rowIndex+"'><input type='checkbox' name='checkbox'></td>";
	
	//产品代码 存在
	cell = tr.insertCell(1);
	cell.innerHTML = "<td><input name='materialID' type='hidden' id='materialID"+rowIndex+"'><input type='text' name='' id='materialCode"+rowIndex+"' size='10' ondblclick='openWinMaterial("+rowIndex+",800,600,1)'></td>" ;
	//产品名称
	cell = tr.insertCell(2);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='' id='materialName"+rowIndex+"'></td>" ;
	//规格型号
	cell = tr.insertCell(3);
	
	cell.innerHTML = "<td><input type='text' size='10' name='' readonly='true' id='spec"+rowIndex+"'></td>" ;
	//辅助属性
	cell = tr.insertCell(4);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='' id='materialAttribute"+rowIndex+"'></td>" ;
	//基本单位名称
	cell = tr.insertCell(5) ;
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name ='' id='basicMeasureUnitName"+rowIndex+"'></td>" ;
	//基本单位数量
	cell = tr.insertCell(6);
	
	cell.innerHTML = "<td><input type='text' size='10' name='basicUnitAmount' id='basicUnitAmount"+rowIndex+"'></td>";

	//单位（*）存在
	cell = tr.insertCell(7);
	
	cell.innerHTML = "<td><input type='hidden' name='measureUnitId' id='measureUnitId"+rowIndex+"'> <input type='text' size='10' name='measureUnit' id='measureUnitName"+rowIndex+"' ondblclick='openWinMeasureUnit(" + rowIndex + ",800,600,0)'></td>";
	
	//数量 存在
	cell = tr.insertCell(8);
	
	cell.innerHTML = "<td><input type='text' size='10' name='amount' id='amount"+rowIndex+"'  onblur='add("+rowIndex+")'></td>" ;
	
	//单价 存在
	cell = tr.insertCell(9);
	
	cell.innerHTML = "<td><input type='text' size='10' name='simplePrice' id='simplePrice"+rowIndex+"'  onblur='add("+rowIndex+")'></td>" ;
	
	//含税单价 存在
	cell = tr.insertCell(10);
	
	cell.innerHTML = "<td><input type='text' size='10' name='includeTaxPrice' id='includeTaxPrice"+rowIndex+"'></td>" ;
	
	//金额 存在
	cell = tr.insertCell(11);
	
	cell.innerHTML = "<td><input type='text' size='10' name='money' id='money"+rowIndex+"'></td>" ;
	
	//税率 存在
	cell = tr.insertCell(12);
	
	cell.innerHTML = "<td><input type='text' size='10' name='taxPercent' id='taxPercent"+rowIndex+"' onblur='add("+rowIndex+")'></td>" ;

	//折扣率 存在
	cell = tr.insertCell(13);
	
	cell.innerHTML = "<td><input type='text' size='10' name='discount' id='discount"+rowIndex+"' onblur='add("+rowIndex+")'></td>" ;
	
	//折扣额 存在
	cell = tr.insertCell(14);
	
	cell.innerHTML = "<td><input type='text' size='10' name='discountPrice' id='discountPrice"+rowIndex+"'></td>" ;
	
	//实际含税单价 存在
	cell = tr.insertCell(15);
	
	cell.innerHTML = "<td><input type='text' size='10' name='acctualIncludeTaxPrice' id='acctualIncludeTaxPrice"+rowIndex+"'></td>" ;
	
	//销项税额 存在
	cell = tr.insertCell(16);
	
	cell.innerHTML = "<td><input type='text' size='10' name='sellTaxMoney' id='sellTaxMoney"+rowIndex+"'></td>" ;
	
	//价税合计 存在
	cell = tr.insertCell(17);
	
	cell.innerHTML = "<td><input type='text' size='10' name='totalMoney' id='totalMoney"+rowIndex+"'></td>" ;
	
	//交货日期 存在
	cell = tr.insertCell(18);
	
	cell.innerHTML = "<td><input type='text' size='10' name='deliveryDate' id='deliveryDate"+rowIndex+"' onclick='time.setHook1(this)'></td>";
	//备注 存在
	cell = tr.insertCell(19);
	
	cell.innerHTML = "<td><input type='text' size='10' name='memo' id='memo"+rowIndex+"'></td>" ;
	//源单单号
	cell = tr.insertCell(20);
	
	cell.innerHTML = "<td><input type='text' size='10' name='sourceBillCode' id='sourceBillCode"+rowIndex+"'></td>" ;
}
function openWinMaterial(index, width, height, scroll) {
	//alert(this.parentElement.parentElement.rowIndex);
	var truelink = "selectMaterialForSellOrder.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}
function childToParentSellOrder(index,id, childId, name, childName, code, childCode, spec, childSpec,materialAttribute,childMaterialAttribute,basicMeasureUnitName,childBasicMeasureUnitName) {
	var t = window.opener.document.getElementById("mbody");	
	//物料id
	var parentId = window.opener.document.getElementById(id +index);
	//物料name
	var parentName = window.opener.document.getElementById(name+ index);
	//物料code
	var parentCode = window.opener.document.getElementById(code + index);
	//规格型号
	var parentSpec = window.opener.document.getElementById(spec+ index);
	//物料属性
	var parentMaterialAttribute = window.opener.document.getElementById(materialAttribute + index);
	//基本计量单位
	var parentBaseMeasureUnitName =  window.opener.document.getElementById(basicMeasureUnitName+ index);
	//销售单位
	//var parentSalesMeasureUnit = window.opener.document.getElementById(salesMeasureUnit + index)
	parentId.value = childId;
	parentName.value = childName;
	parentCode.value = childCode;
	parentSpec.value = childSpec;
	parentMaterialAttribute.value = childMaterialAttribute;
	parentBaseMeasureUnitName.value = childBasicMeasureUnitName;
	//parentSalesMeasureUnit.value = childSalesMeasureUnit
	//基本单位数量
	var basicUnitAmount = window.opener.document.getElementById("basicUnitAmount"+index);
	basicUnitAmount.value='0.0';
	//数量
	var amount = window.opener.document.getElementById("amount"+index);
	amount.value='0.0';
	//单价
	var simplePrice = window.opener.document.getElementById("simplePrice"+index);
	simplePrice.value='0.0';
	//含税单价
	var includeTaxPrice = window.opener.document.getElementById("includeTaxPrice"+index);
	includeTaxPrice.value='0.0';
	//金额
	var money = window.opener.document.getElementById("money"+index);
	money.value='0.0';
	//税率
	var taxPercent = window.opener.document.getElementById("taxPercent"+index);
	taxPercent.value='17.0';
	//折扣率
	 var discount = window.opener.document.getElementById("discount"+index);
	 discount.value='0.0';
	 //折扣额
	 var discountPrice = window.opener.document.getElementById("discountPrice"+index);
	 discountPrice.value='0.0';
	 //实际含税单价
	var acctualIncludeTaxPrice = window.opener.document.getElementById("acctualIncludeTaxPrice"+index);
	acctualIncludeTaxPrice.value='0.0';
	//销项税额
	var sellTaxMoney = window.opener.document.getElementById("sellTaxMoney"+index);
	sellTaxMoney.value='0.0';
	//价税合计
	var totalMoney = window.opener.document.getElementById("totalMoney"+index);
	totalMoney.value='0.0';
	window.close();
}

function openWinMeasureUnit(index, width, height, scroll){
	var truelink = "selectMeasure.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}
//明细--数量/单价事件触发函数
function add(rowIndex){
	//汇率
	var rate = window.document.getElementById('rate');
	var vrate = parseFloat(rate.value);
	//物料数量
	var cost = document.getElementById('amount'+rowIndex);
	var vcost = parseFloat(cost.value);
	
	//物料单价
	var unitPrice = document.getElementById('simplePrice'+rowIndex);
	var vunitPrice = parseFloat(unitPrice.value);
	
	//金额
	var money = document.getElementById('money'+rowIndex);
	var vmoney = parseFloat(money.value);
	//含税单价
	var totalUnitPrice = document.getElementById('includeTaxPrice'+rowIndex);
	var vtotalUnitPrice = parseFloat(totalUnitPrice.value);
	//折扣率
	var discount = document.getElementById('discount'+rowIndex);
	var vdiscount  = parseFloat(discount.value);
	//折扣额
	var discountPrice = document.getElementById('discountPrice'+rowIndex);
	var vdiscountPrice = parseFloat(discountPrice.value);
	//税额
	//var dutyPrice = document.getElementById('dutyPrice'+rowIndex);
	//实际含税单价
	var realTotalUnitPrice = document.getElementById('acctualIncludeTaxPrice'+rowIndex);
	var vrealTotalUnitPrice = parseFloat(realTotalUnitPrice.value);
	//税率
	var dutyFrank = document.getElementById('taxPercent'+rowIndex);
	var vdutyFrank = parseFloat(dutyFrank.value);
	//销项税额
	var sellTaxMoney = document.getElementById('sellTaxMoney'+rowIndex);
	//价税合计
	var totalMoney = document.getElementById('totalMoney'+rowIndex);
	var vtotalMoney = parseFloat(totalMoney.value);
	//价格不为0
	if(vunitPrice>0.0){
		totalUnitPrice.value = vunitPrice*(1+dutyFrank.value/100)/vrate;
	}
	//数量价格都不为0
	if(vcost>0.0 && vunitPrice>0.0){
		//getBasicUnitAmount(rowIndex)
		//getBasicCostInfo(rowIndex);
		
		money.value = vcost*vunitPrice/vrate;
		totalUnitPrice.value = vunitPrice*(1+dutyFrank.value/100)/vrate;
		totalMoney.value = totalUnitPrice.value*vcost/vrate;
		sellTaxMoney.value = money.value*(dutyFrank.value/100)/vrate;
		//有折扣的情况
		if(vdiscount>0.0){
			//实际含税单价
			realTotalUnitPrice.value= vtotalUnitPrice*(1-0.01*vdiscount)/vrate ;
			//价税合计
			totalMoney.value = realTotalUnitPrice.value*vcost/vrate ;
			//折扣额
			discountPrice.value = vcost*totalUnitPrice.value*(vdiscount/100)/vrate;
			//销项税额= 金额*税率
			sellTaxMoney.value = money.value*(dutyFrank.value/100)/vrate;
			//税额
			//dutyPrice.value = vcost*vunitPrice*(1-0.01*vdiscount)*dutyFrank.value/100;						
		}
		
	}
}

	
	

